﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Ext.Net;
using Biz;
using Biz.UserManagement;
using Biz.Factory;
using Biz.Status;

namespace Web
{
    public partial class PresentList : PageHelper
    {
        public string EMPLOYEE_TYPE
        {
            get
            {
                return HttpContext.Current.Request.QueryString["type"].Trim();
            }
        }

        public PresentApplication CURRENT_SELECT_APPLICATION
        {
            get
            {
                if (RowSelectionModel1.SelectedRecordID == null || RowSelectionModel1.SelectedRecordID.Trim() == "")
                {
                    return null;
                }
                PresentApplication app = (PresentApplication)Factory.getInstance().CreateProduct(Biz.Application.ApplicationType.PRESENT.ToString());
                app.load(int.Parse(RowSelectionModel1.SelectedRecordID.Trim()));
                return app;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                if (CurrentUser == null)
                    return;
                init();
            }
        }

        private void init()
        {
            wContent.Hidden = true;
            switch (EMPLOYEE_TYPE)
            {
                case "applicant":
                    btnAdd.Hidden = false;
                    btnEdit.Hidden = false;
                    btnCancel.Hidden = false;
                    btnBack.Hidden = true;
                    btnSubmit.Text = "提交";
                    gpData.Store.Primary.DataSource = new Applicant(CurrentUser.Id).ListAllPresentApplication();
                    break;

                case "approver":
                    btnAdd.Hidden = true;
                    btnEdit.Hidden = false;
                    btnCancel.Hidden = false;
                    btnBack.Hidden = false;
                    btnSubmit.Text = "审核";
                    gpData.Store.Primary.DataSource = new Approver(CurrentUser.Id).ListAllPresentApplication();
                    break;

                case "ga":
                    btnAdd.Hidden = true;
                    btnEdit.Hidden = false;
                    btnEdit.Text = "查看";
                    btnCancel.Hidden = true;
                    btnSubmit.Text = "支付";
                    tApplicant.ReadOnly = true;
                    tApprover.ReadOnly = true;
                    tActualFee.ReadOnly = true;
                    tGA.ReadOnly = true;
                    tStatus.ReadOnly = true;
                    tTotalAmount.ReadOnly = true;
                    tNumOfPerson.ReadOnly = true;
                    tRemarks.ReadOnly = true;
                    tOpposite.ReadOnly = true;
                    dExcuteDate.ReadOnly = true;
                    btnBack.Hidden = true;
                    gpData.Store.Primary.DataSource = new GAUser(CurrentUser.Id).ListAllPresentApplication();
                    break;
            }
            gpData.Store.Primary.DataBind();

        }

        protected void Add_Click(object sender, DirectEventArgs e)
        {
            wContent.Hidden = false;

            tApplicant.Text = CurrentUser.UserCName;
            tApprover.Text = new Employee(CurrentUser.DefaultApproveID).UserCName;
            tActualFee.Text = "";
            tGA.Text = "";
            tStatus.Text = "";
            tTotalAmount.Text = "";
            tNumOfPerson.Text = "";
            tRemarks.Text = "";
            tOpposite.Text = "";
            dExcuteDate.Text = DateTime.Today.ToString("yyyy-MM-dd");
            Session["CurrentPresent"] = null;
        }

        protected void Edit_Click(object sender, DirectEventArgs e)
        {
            PresentApplication a = CURRENT_SELECT_APPLICATION;
            if (a == null)
            {
                X.Msg.Alert("提示", "请先选择一条记录").Show();
                return;
            }
            wContent.Hidden = false;
            tApplicant.Text = new Employee(a.ApplicantID.Value).UserCName;
            tApprover.Text = new Employee(a.ApproverID.Value).UserCName;
            tActualFee.Text = a.ActualFee.ToString();
            tGA.Text = a.GaUser == null ? "" : new Employee(a.GaUser.Value).UserCName;
            tStatus.Text = a.Status.ToString();
            tTotalAmount.Text = a.TotalAmount.ToString();
            tNumOfPerson.Text = a.NumOfPerson.ToString();
            tRemarks.Text = a.Remarks.ToString();
            tOpposite.Text = a.Opposite.ToString();
            tPresent.Text = a.Present;
            dExcuteDate.Text = a.ExcuteDate.ToString("yyyy-MM-dd");
            Session["CURRENT_CHECK_LIST"] = a.CheckList;
            Session["CurrentPresent"] = a;
        }

        protected void Cancel_Click(object sender, DirectEventArgs e)
        {
            PresentApplication a = CURRENT_SELECT_APPLICATION;
            if (a == null)
            {
                X.Msg.Alert("提示", "请先选择一条记录").Show();
                return;
            }
            a.Status.cancel();
            a.save();
        }

        protected void Save_Click(object sender, DirectEventArgs e)
        {
            PresentApplication a = null;
            if (Session["CurrentPresent"] == null)
                a = (PresentApplication)Factory.getInstance().CreateProduct(Biz.Application.ApplicationType.PRESENT.ToString());
            else
                a = (PresentApplication)Session["CurrentPresent"];

            a.ApplicantID = Employee.GetEmployee(tApplicant.Text).Id;
            a.ApproverID = Employee.GetEmployee(tApprover.Text).Id;
            a.GaUser = Employee.GetEmployee(tGA.Text).Id;
            a.Status = (Biz.Status.ApplicationStatus)Factory.getInstance().CreateProduct(tStatus.Text);
            a.Present = tPresent.Text;
            if (Session["CURRENT_CHECK_LIST"] != null)
            {
                a.CheckList = (Biz.Check.CheckList)Session["CURRENT_CHECK_LIST"];
                a.CheckList.save();
            }
            a.Remarks = tRemarks.Text;
            a.Opposite = tOpposite.Text;
            a.ExcuteDate = dExcuteDate.SelectedDate;
        }

        protected void Submit_Click(object sender, DirectEventArgs e)
        {
            PresentApplication a = null;
            if (Session["CurrentPresent"] == null)
                a = (PresentApplication)Factory.getInstance().CreateProduct(Biz.Application.ApplicationType.PRESENT.ToString());
            else
                a = (PresentApplication)Session["CurrentPresent"];
            Save_Click(null, null);
            a.Status = a.Status.next();
            a.save();
        }

        protected void Back_Click(object sender, DirectEventArgs e)
        {
            PresentApplication a = null;
            if (Session["CurrentPresent"] == null)
                a = (PresentApplication)Factory.getInstance().CreateProduct(Biz.Application.ApplicationType.PRESENT.ToString());
            else
                a = (PresentApplication)Session["CurrentPresent"];
            a.Status = a.Status.back();
            a.save();
        }

        protected void CheckList_Click(object sender, DirectEventArgs e)
        {
            wChecklist.Hidden = false;
            wChecklist.ResolveUrl("CheckListVIew.aspx");
        }
    }
}